package com.yzs.core.http

/**
 *    author : yzs
 *    date   : 2022/1/1211:50
 *    desc   : 响应拦截器 判断异常上报
 */
//class ApiExceptionInterceptor : Interceptor {
//    override fun intercept(chain: Interceptor.Chain): Response {
//        val request = chain.request()
//        val response = chain.proceed(request)
//
//        val builder: Response.Builder = response.newBuilder()
//        val clone: Response = builder.build()
//
//        val url = request.url.toString()
//
//        // 1.0.5版本之前 处理 url，只上传接口部分, <= 之后全部参数上传
//        var formatUrl = url
////        val indexOfFirst = formatUrl.indexOfFirst { it == '?' }
////        if (indexOfFirst > 0) {
////            val baseUrl = AppManager.baseUrl
////            val startIndex = if (formatUrl.contains(baseUrl)) baseUrl.length else 0
////            formatUrl = url.subSequence(startIndex, indexOfFirst).toString()
////        }
//
//
//        val code = clone.code
//        if (code != 200) { // 服务器异常 上报
//            // 如果 uid 获取不到 就获取手机唯一码 也有可能是 ""
//            val uid = AppManager.uid
//            val reportId = if (uid == 0L) DeviceUtils.getUniqueDeviceId() else uid.toString()
//            val throwable = Throwable(
//                "id:【 $reportId 】ApiExceptionInterceptor [Http code != 200] : msg --> ${response.message} , code --> $code , url --> $formatUrl  phoneInfo --> 【 $MANUFACTURER $MODEL $RELEASE 】 time --> ${TimeUtils.getNowString()}"
//            )
//            CrashReport.postCatchedException(throwable)
//        } else if (code == 200) { // 请求是正常的 ，处理 业务异常 上报
//            var flag = false // Just for print log
//            var body = clone.body
//            if (body != null) {
//                val mediaType = body.contentType()
//                if (mediaType != null) {
//                    try {
//                        val resp = body.string()
//                        body = resp.toResponseBody(mediaType)
//                        flag = true
//                        val apiResponse = GsonUtils.fromJson(resp, ApiResponse2::class.java)
//                        if (apiResponse != null) {
//                            val apiCode = apiResponse.code
//                            if (apiCode != 200) { // 只处理异常情况
//                                // 如果 uid 获取不到 就获取手机唯一码 也有可能是 ""
//                                val uid = AppManager.uid
//                                val reportId =
//                                    if (uid == 0L) DeviceUtils.getUniqueDeviceId() else uid.toString()
//                                val throwable = Throwable(
//                                    "id:【 $reportId 】ApiExceptionInterceptor [Http code == 200] : msg --> ${apiResponse.msg} , code --> $apiCode , url --> $formatUrl   phoneInfo --> 【 $MANUFACTURER $MODEL $RELEASE 】 time --> ${TimeUtils.getNowString()}"
//                                )
//                                CrashReport.postCatchedException(throwable)
//                            }
//                        }
//                    } catch (e: Exception) {
//                        e.printStackTrace()
//                        LogUtils.d("拦截响应 --- 解析异常 ${e.message}")
//                    }
//                }
//            }
//            if (flag) return response.newBuilder().body(body).build()
//        }
//        return response
//    }
//
//}